/*
 * @Description: 异步并行钩子
 * @Author: wangqi
 * @Date: 2021-04-17 12:05:11
 * @LastEditTime: 2021-04-18 11:05:44
 */

class AsyncParallelHook {
    constructor(args) {
        this.hooks = [];
    }

    tapPromise(name, task) {
        this.hooks.push(task);
    }

    // 当所有 tapPromise注册的钩子都执行完后，再执行最后的回调
    promise(...res) {
        let hooks = this.hooks.map(hook => hook(...res));
        return Promise.all(hooks)
    }

}

let asyncparallelhook = new AsyncParallelHook(['name']);
asyncparallelhook.tapPromise('node', (name) => {
    return new Promise((resolve, reject) => {
        setTimeout(() => {
            console.log('node', name);
            resolve();
        }, 1000);
    })
});

asyncparallelhook.tapPromise('vue', (name) => {
    return new Promise((resolve, reject) => {
        setTimeout(() => {
            console.log('vue', name)
            resolve();
        }, 2000);
    })

});

asyncparallelhook.promise('wq').then(function () {
    console.log("end~~")
});


